<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>sign in</title>
    <link rel="stylesheet" href="../static/assets/bootstrap/css/bootstrap.min.css"
          th:href="@{/assets/bootstrap/css/bootstrap.min.css}"/>
    <style type="text/css">
    </style>
</head>

<body>
<section>
    <div class="container mt-5">
        <div class="row">
            <div class="col-sm-3">

            </div>
            <div class="col-sm-6">
                <form id="signInForm" action="/security_check" method="post">
                    <h3 class="text-center">登录</h3>
                    <p class="text-center alert alert-danger" th:classappend="${error==null || error==''?'invisible':''}" th:text="${error}"></p>
                    <div class="form-group row">
                        <label for="username" class="col-3 col-form-label text-right">用户名</label>
                        <div class="col-9">
                            <input type="text" name="username" class="form-control" id="username" required="required"
                                   placeholder="用户名"/>
                            <i class="fa fa-user"></i>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="password" class="col-3 col-form-label text-right">密　码</label>
                        <div class="col-9">
                            <input type="password" name="password" class="form-control" id="password"
                                   required="required"
                                   placeholder="密　码"/>
                            <i class="fa fa-lock"></i>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="verificationCode" class="col-3 col-form-label text-right">验证码</label>
                        <div class="col-4">
                            <input type="text" name="verificationCode" class="form-control" id="verificationCode"
                                   required="required"
                                   placeholder="验证码"/>
                            <i class="fa fa-lock"></i>
                        </div>
                        <div class="col-5">
                            <img src="/captcha?ts=1" id="captchaImg" style="cursor: pointer;" class="img-fluid"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-3 col-form-label text-right"></label>
                        <div class="col-9">
                            <div class="main-checkbox">
                                <input type="checkbox" id="rememberUsername" name="check"/>
                                <label for="rememberUsername">记住用户名</label>
                            </div>
                            <button type="submit" class="btn btn-primary">登录</button>
                            <a class="btn btn-sm" href="/signUp">注册</a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-sm-3">

            </div>
        </div>
    </div>
</section>
<script src="../static/assets/jquery-3.3.1.min.js"
        th:src="@{/assets/jquery-3.3.1.min.js}"></script>
<script src="../static/assets/bootstrap/js/bootstrap.min.js"
        th:src="@{/assets/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/assets/localforage.min.js}" src="../static/assets/localforage.min.js"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    $(document).ready(function () {

        localforage.getItem('username').then(function (value) {
            // This code runs once the value has been loaded
            // from the offline store.
            if (value != null && value != "") {
                $("#username").val(value);
                $("#rememberUsername").prop("checked", "checked");
                $("#password").focus();
            }

        }).catch(function (err) {
            // This code runs if there were any errors
            console.log(err);
        });

        $("#captchaImg").on("click", function () {
            var timestamp = (new Date()).valueOf();
            $("#captchaImg").attr("src", "/captcha?ts=" + timestamp);
        })

        $("#signInForm").on("submit", function () {

            if ($("#rememberUsername").prop("checked")) {
                localforage.setItem('username', $("#username").val()).then(function (value) {
                    // Do other things once the value has been saved.
                }).catch(function (err) {
                    // This code runs if there were any errors
                    console.log(err);
                });
            } else {
                localforage.removeItem('username').then(function (value) {
                    // Do other things once the value has been saved.
                }).catch(function (err) {
                    // This code runs if there were any errors
                    console.log(err);
                });
            }


            $(this).parents("#signInForm").submit();
        })
    });
    /*]]>*/
</script>

</body>
</html>